package com.da.lon.wang.xlg.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.bean.ResultSearchBean.ResultSearchListBean.SearchResultOther.SearchResultOtherItems;

import java.util.ArrayList;

public class FilterGridAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private Context context;
    private ArrayList<SearchResultOtherItems> itemslist;
    private String attr = "";

    public FilterGridAdapter(Context context,
                             ArrayList<SearchResultOtherItems> itemslist) {
        this.context = context;
        this.itemslist = itemslist;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return itemslist != null ? itemslist.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder5 holder = new Holder5();

        try {
            convertView = inflater.inflate(R.layout.ouka_filter_item_gv_item, null);

            SearchResultOtherItems resultOtherItems = itemslist.get(position);

            holder.itemTextView = (TextView) convertView
                    .findViewById(R.id.ouka_filter_gv_item_tv);
            holder.itemTextView.setText(resultOtherItems.getId());

            if (resultOtherItems != null && resultOtherItems.isSelect() == true) {
                holder.itemTextView
                        .setBackgroundResource(R.drawable.ouka_filter_select_check);
                holder.itemTextView.setTextColor(Color.parseColor("#FF0000"));
            } else {
                holder.itemTextView
                        .setBackgroundResource(R.drawable.ouka_filter_select_normal);
                holder.itemTextView.setTextColor(Color.parseColor("#333333"));
            }

            if (convertView.getTag() == null) {
                convertView.setTag(holder);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        return convertView;
    }

    //最多只能选择5个属性
    public boolean chooceState(int position, int index) {
        int i = 0;
        boolean flger = false;
        try {
            itemslist = MyApplication.getInstance().getOtherlist().get(position - 2).getItems();
            if (MyApplication.getInstance().getOtherlist().get(position - 2).getItems().get(index).isSelect() == true) {
                MyApplication.getInstance().getOtherlist().get(position - 2).getItems().get(index).setSelect(false);
                this.notifyDataSetChanged();
                flger = false;
            } else {
                for (SearchResultOtherItems iterable_element : itemslist) {
                    if (iterable_element.isSelect()) {
                        i++;
                    }
                }
                if (i > 4) {
                    flger = true;
                } else {
                    MyApplication.getInstance().getOtherlist().get(position - 2).getItems().get(index).setSelect(true);
                    this.notifyDataSetChanged();
                    flger = false;
                }
            }


        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return flger;

    }
}

class Holder5 {

    TextView itemTextView;
}
